package jp.idahopotato.activity;

import jp.matome.activity.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

public class TypeSetActivity extends Activity {
	private static final String FILE_NAME = "PreferenceTypeSetFile";
	private static int lowerSelected = 1;
	private static int upperSelected = 7;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.typeset);

		Intent data = getIntent();
		Bundle extras = data.getExtras();
		String str = extras.getString("SELECT_MENU");

		TextView title = (TextView)findViewById(R.id.text_type);
		title.setText(str);

		final SharedPreferences pref = getSharedPreferences(FILE_NAME, MODE_PRIVATE);

		final RadioButton kensi = (RadioButton)findViewById(R.id.kensi);
		final RadioButton gunner = (RadioButton)findViewById(R.id.gunner);
		final RadioButton male = (RadioButton)findViewById(R.id.male);
		final RadioButton female = (RadioButton)findViewById(R.id.female);

		final RadioButton wep0 = (RadioButton)findViewById(R.id.wep0);
		final RadioButton wep1 = (RadioButton)findViewById(R.id.wep1);
		final RadioButton wep2 = (RadioButton)findViewById(R.id.wep2);
		final RadioButton wep3 = (RadioButton)findViewById(R.id.wep3);

		final Spinner lowerRare = (Spinner)findViewById(R.id.lowerrare);
		final Spinner upperRare = (Spinner)findViewById(R.id.upperrare);

		String type = pref.getString("type", "ガンナー");
		String gender = pref.getString("gender", "女性");
		int wep = pref.getInt("wep", 0);
		int lower = pref.getInt("lower", 1);
		lowerSelected = lower;
		int upper = pref.getInt("upper", 7);
		upperSelected = upper;

		if(type.equals("ガンナー")){
			kensi.setChecked(true);
		} else {
			gunner.setChecked(true);
		}

		if(gender.equals("女性")){
			male.setChecked(true);
		} else {
			female.setChecked(true);
		}

		if(wep == 0){
			wep0.setChecked(true);
		} else if(wep == 1){
			wep1.setChecked(true);
		} else if(wep == 2){
			wep2.setChecked(true);
		} else if(wep == 3){
			wep3.setChecked(true);
		}

		lowerRare.setSelection(lower-1);
		upperRare.setSelection(upper-1);

		lowerRare.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO 自動生成されたメソッド・スタブ
				Spinner spinner = (Spinner)parent;
				int ls = Integer.parseInt((String) spinner.getItemAtPosition(position));
				lowerSelected = ls;
				if(ls > upperSelected){
					upperRare.setSelection(ls-1);
					upperSelected = ls;
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO 自動生成されたメソッド・スタブ
				return;
			}
		});

		upperRare.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO 自動生成されたメソッド・スタブ
				Spinner spinner = (Spinner)parent;
				int us = Integer.parseInt((String) spinner.getItemAtPosition(position));
				upperSelected = us;
				if(us < lowerSelected){
					lowerRare.setSelection(us-1);
					lowerSelected = us;
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO 自動生成されたメソッド・スタブ
				return;
			}
		});

		Button button = (Button)findViewById(R.id.backbutton_type);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				SharedPreferences.Editor editor = pref.edit();

				if(kensi.isChecked()){
					editor.putString("type", "ガンナー");
				} else if(gunner.isChecked()){
					editor.putString("type", "剣士");
				}

				if(male.isChecked()){
					editor.putString("gender", "女性");
				} else if(female.isChecked()){
					editor.putString("gender", "男性");
				}

				if(wep0.isChecked()){
					editor.putInt("wep", 0);
				} else if(wep1.isChecked()){
					editor.putInt("wep", 1);
				} else if(wep2.isChecked()){
					editor.putInt("wep", 2);
				} else if(wep3.isChecked()){
					editor.putInt("wep", 3);
				}

				editor.putInt("lower", Integer.parseInt((String)lowerRare.getSelectedItem()));
				editor.putInt("upper", Integer.parseInt((String)upperRare.getSelectedItem()));

				editor.commit();

				Intent intent = getIntent();
				intent.putExtra("RETURN_FLAG", "タイプ設定から");

				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}


}
